package com.mall.user.services;

import com.mall.user.IUserLoginService;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.converter.UserConverterMapper;
import com.mall.user.dal.entitys.Member;
import com.mall.user.dal.entitys.User;
import com.mall.user.dal.entitys.UserVerify;
import com.mall.user.dal.persistence.MemberMapper;
import com.mall.user.dal.persistence.UserVerifyMapper;
import com.mall.user.dto.*;
import com.mall.user.utils.AESUtil;
import com.mall.user.utils.ExceptionProcessorUtils;
import com.mall.user.utils.JwtTokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * 创建日期: 2022-01-19-21-41
 * author : LL
 * description:
 */
@Slf4j
@Component
@Service
public class UserLoginServiceImpl implements IUserLoginService {
    @Autowired
    MemberMapper memberMapper;

    @Autowired
    UserVerifyMapper userVerifyMapper;

    @Autowired
    UserConverterMapper userConverterMapper;

    @Override
    public CheckAuthResponse validToken(CheckAuthRequest checkAuthRequest) {
        CheckAuthResponse authResponse = new CheckAuthResponse();
        try {
            JwtTokenUtils freeJWTTool = JwtTokenUtils.builder().token(checkAuthRequest.getToken()).build();
            String userInfo = freeJWTTool.freeJwt();
            authResponse.setUserinfo(userInfo);
            authResponse.setCode(SysRetCodeConstants.SUCCESS.getCode());
            authResponse.setMsg(SysRetCodeConstants.SUCCESS.getMessage());
        }catch (Exception e){
            authResponse.setMsg(SysRetCodeConstants.TOKEN_VALID_FAILED.getMessage());
            authResponse.setCode(SysRetCodeConstants.TOKEN_VALID_FAILED.getCode());
            log.error("UserLoginServiceImpl.validToken occur Exception :" + e);
            ExceptionProcessorUtils.wrapperHandlerException(authResponse,e);
        }
        return authResponse;
    }

    @Override
    public UserinfoResponse verifyLogin(UserinfoRequest userinfoRequest) {
        UserinfoResponse userinfoResponse = new UserinfoResponse();
        try {
            userinfoRequest.requestCheck();
            Long uid = userinfoRequest.getUid();
            Member member = memberMapper.selectByPrimaryKey(uid);
            userinfoResponse.setCode(SysRetCodeConstants.SUCCESS.getCode());
            userinfoResponse.setMsg(SysRetCodeConstants.SUCCESS.getMessage());
            userinfoResponse.setUid(uid);
            userinfoResponse.setFile(member.getFile());
        }catch (Exception e){
            log.error("UserLoginServiceImpl.verifyLogin occur Exception :" + e);
            ExceptionProcessorUtils.wrapperHandlerException(userinfoResponse,e);
        }
        return userinfoResponse;
    }

    @Override
    public UserVerifyResponse userVerify(UserVerifyRequest userVerifyRequest) {
        UserVerifyResponse userVerifyResponse = new UserVerifyResponse();
        try {
            userVerifyRequest.requestCheck();
            Example example = new Example(UserVerify.class);
            example.createCriteria().andEqualTo("uuid",userVerifyRequest.getUid()).andEqualTo("username",userVerifyRequest.getUsername());
            List<UserVerify> userVerifies = userVerifyMapper.selectByExample(example);
            if (userVerifies == null || userVerifies.size() == 0){
                userVerifyResponse.setCode(SysRetCodeConstants.USERVERIFY_INFOR_INVALID.getCode());
                userVerifyResponse.setMsg(SysRetCodeConstants.USERVERIFY_INFOR_INVALID.getMessage());
            }
            memberMapper.updateVerifyByUsername(userVerifyRequest.getUsername());
            userVerifyMapper.updateVerifyByUsername(userVerifyRequest.getUsername());
            userVerifyResponse.setCode(SysRetCodeConstants.SUCCESS.getCode());
            userVerifyResponse.setMsg(SysRetCodeConstants.SUCCESS.getMessage());

        }catch (Exception e){
            log.error("UserLoginServiceImpl.userVerify occur Exception :" + e);
            ExceptionProcessorUtils.wrapperHandlerException(userVerifyResponse,e);
        }
        return userVerifyResponse;
    }

    @Override
    public UserLoginResponse userLogin(UserLoginRequest loginRequest) {
        // String encryptPassword = new AESUtil(loginRequest.getPassword()).encrypt();
        String encryptPassword = DigestUtils.md5DigestAsHex(loginRequest.getPassword().getBytes());
        System.out.println(encryptPassword);
        Example example = new Example(User.class);
        example.createCriteria().andEqualTo("email",loginRequest.getUserName()).andEqualTo("password",encryptPassword);
        List<Member> members = memberMapper.selectByExample(example);
        UserLoginResponse loginResponse = userConverterMapper.converter(members.get(0));
        if (members == null || members.size() == 0){
            loginResponse.setCode(SysRetCodeConstants.USERORPASSWORD_ERRROR.getCode());
            loginResponse.setMsg(SysRetCodeConstants.USERORPASSWORD_ERRROR.getMessage());
            return loginResponse;
        }
        loginResponse.setCode(SysRetCodeConstants.SUCCESS.getCode());
        loginResponse.setCode(SysRetCodeConstants.SUCCESS.getMessage());
        return loginResponse;
    }


}